﻿define(['durandal/app', 'durandal/system', 'knockout','jquery','Util/bootstrap-datepicker'], function (app, system, ko,$,datePicker) {


//    var EventModel = function () {
//        var self = this;
//        self.eventName = ko.observable('');
//        self.description = ko.observable('');
//        self.eventDate = ko.observable('');
//        self.recipientUser = ko.observable('');// SINGLE USERDTO
//        self.inviteeList = ko.observable([]);//LIST OF USERDTO or list of emails
//        self.createdByUser = ko.observable('');// Single User DTO
//        self.eventLockDate = ko.observable('');
//        self.fromMessage = ko.observable('');
//        self.channel = ko.observable('');//enum
//        self.enableReminder = ko.observable('');//true or false
//        return self;
//        
//    };

    var eventModel = new app.dto.EventModel();
    
    eventModel.inviteesInput = ko.observable('');
   
    var eventDatePicker;
    var eventLockDatePicker;
    var checkEventForm = function (eventModelItem) {

//       
//            eventModelItem.createdByUser = {
//            		userId : 1, 
//            		emailId : 'madhured@gmail.com',
//            		firstName : 'Madhukar', 
//            		lastName : 'Anugu', 
//            		mobileNumber : null, 
//            		userStatus : 0
//            		} ;
//            eventModelItem.eventName = 'UI Birthday Test';
//            eventModelItem.channel = "web";
//            eventModelItem.eventDate = '2013-10-15';
//            
//            eventModelItem.inviteeList = [{userId : 1, emailId : 'madhured@gmail.com', firstName : 'Madhukar', lastName : 'Anugu', mobileNumber : null, userStatus : 0}];
//            eventModelItem.recipientUser = [{userId : 1, emailId : 'madhured@gmail.com', firstName : 'Madhukar', lastName : 'Anugu', mobileNumber : null, userStatus : 0}];
//            
//            eventModelItem.enableReminder = true;
//            eventModelItem.recepientCountry = 'USA';
//            eventModelItem.eventLockDate = '2013-10-13';
                       
    	var inviteArrayList = eventModelItem.inviteesInput.toString().split(',');
    	
    	var newInviteeList = [];
    	$.each(inviteArrayList, function( index, value ) {
    		  var emailObject = {"emailId":value};
    		  newInviteeList.push(emailObject);
    		});
        eventModelItem.inviteeList = newInviteeList; //{"emailId":"dummy@gmail.com"}  inviteArrayList;
        delete eventModelItem['inviteesInput'];
        
        
        eventModelItem.createdByUser = {"userId": 1};//// Single User DTO
        eventModelItem.channel = 'WEB';
        eventModelItem.enableReminder = $("#enableReminder").is(':checked');
         var userInputRecipientUser = eventModelItem.recipientUser;
         
        eventModelItem.recipientUser = {"emailId": userInputRecipientUser};
        eventModelItem.eventLockDate = eventLockDatePicker.data().datepicker.date.valueOf();//.format("yyyy-MM-dd");//.valueOf();//  eventLockDatePicker.date.valueOf();
        eventModelItem.eventDate = eventDatePicker.data().datepicker.date.valueOf();//.format("yyyy-MM-dd"); //.valueOf();//eventDatePicker.date.valueOf();
       // mm-dd-yyyy
       // eventModelItem.eventLockDate = null;
        //eventModelItem.eventDate = null;
//    	eventModelItem = {
//    		 "eventName": "Test Name",
//    		   "description": "Test Description",
//    		   "recipientUser": null,
//    		   "createdByUser": {"userId": 1},
//    		   "eventLockDate": null,
//    		   "fromMessage": "Testing mesg",
//    		 "channel" : "ANDROID",
//    		 "inviteeList": [{"emailId":"dummy@gmail.com"}]
//    		};
    	
        

        //Do Validations Here
        return eventModelItem;
    };

    var createViewAttached = function (view, parent) {
    	
	    
	    eventDatePicker =    $(view).find('#eventDateContainer .input-append.date').datepicker({
	  
	    autoclose: true,
	    todayHighlight: true
	});
	

	    eventLockDatePicker = 	 $(view).find('#eventLockDateContainer .input-append.date').datepicker({
	    
	    autoclose: true,
	    todayHighlight: true
	});
	 
	 
    system.log('Lifecycle : attached : hello');
   };
    var createEventService = function () {
        var eventItem = ko.toJS(eventModel);
        eventItem = checkEventForm(eventItem);

      //  var sendEventItem = { eventCreationRequest: eventItem };
        //appModel.ajaxService.ajaxPostJson("createEvent", sendEventItem, onCreateEventResult);
        appModel.ajaxService.ajaxPostJson("createEvent", eventItem, onCreateEventResult);
    };

    var onCreateEventResult = function (result) {

        alert('Result');

        //     dtoKeyId = resultJson.Data;
        //    var url = '#/Candidates';
        //   router.navigateTo(url);

        // router.navigateBack();
        // return router.activate('Candidates');

        // toastr.success('Success', 'Candidate Added');
    };


 
    return {
        eventModel: eventModel,
        checkEventForm: checkEventForm,
        createEventService: createEventService,
        onCreateEventResult: onCreateEventResult,
      
        activate: function() {
            system.log('Lifecycle : activate : hello');
        },
        binding: function () {
            system.log('Lifecycle : binding : hello');
            return { cacheViews:false }; //cancels view caching for this module, allowing the triggering of the detached callback
        },
        bindingComplete: function () {
            system.log('Lifecycle : bindingComplete : hello');
        },
        attached: createViewAttached,
        compositionComplete: function (view) {
            system.log('Lifecycle : compositionComplete : hello');
        },
        detached: function (view) {
            system.log('Lifecycle : detached : hello');
        }
    };
   
});

//requirejs('hello/index').event.channel()